#include "e2d/yml/impl/Serializer.h"
#include "e2d/core/impl/components.h"

using Serializer = e2d::BasicSerializer;

template<>
 void Serializer::Encode<e2d::Scene>(YAML::Node& node, const e2d::Scene& inst)
{
	Encode<e2d::Object>(node, inst);
	SetVariable<std::string>(node, "name", inst.m_Name);
	SetVariable<e2d::EntityMap>(node, "m_EditingCamera", inst.m_EditingCamera);
	SetVariable<e2d::EntityMap>(node, "m_EditingEntity", inst.m_EditingEntity);
	SetVariable<e2d::EntityMap>(node, "m_MainCamera", inst.m_MainCamera);
	SetPtrArrayVariable<e2d::Entity>(node, "entities", inst.ExistEntities());
}

template<>
 bool Serializer::Decode<e2d::Scene>(const YAML::Node& node, e2d::Scene& inst)
{
	Decode<e2d::Object>(node, inst);
	GetVariable<std::string>(node, "name", inst.m_Name);
	GetVariable<e2d::EntityMap>(node, "m_EditingCamera", inst.m_EditingCamera);
	GetVariable<e2d::EntityMap>(node, "m_EditingEntity", inst.m_EditingEntity);
	GetVariable<e2d::EntityMap>(node, "m_MainCamera", inst.m_MainCamera);

	{
		inst.m_Entities.clear();

		YAML::Node childNode = node["entities"];
		if (childNode.IsDefined())
		{
			for (auto entityNode : childNode)
			{
				auto newEntity = inst.NewEntity();
				newEntity->m_Parent = 0;
				newEntity->m_Scene = &inst;
				inst.m_Entities.push_back(newEntity);

				Decode(entityNode, *newEntity);
			}
		}
		else
		{
			ztclog::info("failed get childnode");
		}

	}


	return true;
}
